﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using StateGrid.GlobalVariables;
using StateGrid;
using StateGrid.User;

public abstract partial class AddCatalogBasePage : CommonPage
{
    // 在子类中实现以下方法
    protected abstract void DoInit();
    protected abstract void DoCheck(string parent, Catalog candidate);
    protected abstract void DoCommit(Catalog candidate);

    // master控件
    private TextBox nameTextBox;
    private TextBox codeTextBox;
    private Button submitButton;
    private Button resetButton;
    private Label errorLabel;

    // 参数
    private string parentCode;

    protected void Page_Load(Object sender, EventArgs e)
    {
        if (!IsLogIn())
        {
            return;
        }
        DoInit();
        
        InitControls();

        ReloadData();

        if (!IsPostBack)
            codeTextBox.Text = parentCode;
    }

    /// <summary>
    /// 判断用户是否登录
    /// </summary>
    private bool IsLogIn()
    {
        bool flag = true;
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            flag = false;
        }
        return flag;
    }

    private void InitControls()
    {
        nameTextBox = Master.FindControl("nameTextBox") as TextBox;
        codeTextBox = Master.FindControl("codeTextBox") as TextBox;
        submitButton = Master.FindControl("submitButton") as Button;
        resetButton = Master.FindControl("resetButton") as Button;
        errorLabel = Master.FindControl("errorLabel") as Label;

        submitButton.Click += new EventHandler(SubmitButton_Click);
        resetButton.Click += new EventHandler(ResetButton_Click);
    }

    private void ReloadData()
    {
        if (Request["parent"] != null && Request["parent"] != "")
            parentCode = Request["parent"];
        else
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=出错啦");
    }

    void SubmitButton_Click(object sender, EventArgs e)
    {
        try
        {
            CheckNotEmpty(nameTextBox.Text, "分类名称不能为空");
            CheckNotEmpty(codeTextBox.Text, "分类编码不能为空");
            Check(codeTextBox.Text.StartsWith(parentCode), "分类编码必须以父类编码开头");
            Check(parentCode != codeTextBox.Text, "子类编码不能和父类相同");
            
            Catalog catalog = new Catalog()
            {
                Name = nameTextBox.Text,
                Code = codeTextBox.Text
            };
            DoCheck(parentCode, catalog);
            DoCommit(catalog);

            Response.Write("<Script>alert('添加分类成功')</Script>");
        }
        catch (Exception exception)
        {
            errorLabel.Text = exception.Message;
        }

    }

    void ResetButton_Click(object sender, EventArgs e)
    {
        nameTextBox.Text = "";
        codeTextBox.Text = parentCode;
    }
}